﻿namespace TestTool.GUI.Controls
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Net;
    using System.Net.Sockets;
    using System.ServiceModel;
    using System.Windows.Forms;
    using TestTool.GUI.Controllers;
    using TestTool.GUI.Enums;
    using TestTool.GUI.Utils;
    using TestTool.GUI.Views;
    using TestTool.Tests.Common.Discovery;
    using TestTool.Tests.Definitions.Data;

    internal class DiscoveryPage : Page, IDiscoveryView, IView
    {
        private bool _autoSelectDevice;
        private DiscoveryController _controller;
        private string _prevAddress;
        private string _prevDeviceIp;
        private bool _probeAnswered;
        private const string _serviceDataEmpty = "<Press Check to get information>";
        private bool _unicastProbing;
        private Button btnCheckIP;
        private Button btnCheckService;
        private Button btnClear;
        private Button btnDiscover;
        private CheckBox checkHWStyle;
        private CheckBox chkShowSearchOptions;
        private ComboBox cmbNICs;
        private ComboBox cmbServiceAddress;
        private IContainer components;
        private ToolTip discoveryPageToolTip;
        private GroupBox gbDutInfo;
        private ColumnHeader hdrIP;
        private ColumnHeader hdrUUID;
        private Label lblBrand;
        private Label lblDeviceIP;
        private Label lblDeviceServiceAddress;
        private Label lblEpAddress;
        private Label lblFirmwareVersion;
        private Label lblMetadataVersion;
        private Label lblModel;
        private Label lblNIC;
        private Label lblPassword;
        private Label lblScopes;
        private Label lblSearchScopes;
        private Label lblSerial;
        private Label lblType;
        private Label lblUsername;
        private ListView lvDevices;
        private Panel panelDiscovery;
        private Panel panelScopes;
        private Panel panelSearch;
        private SplitContainer splitContainerDiscovery;
        private TextBox tbBrand;
        private TextBox tbDeviceIP;
        private TextBox tbEpAddress;
        private TextBox tbFirmwareVersion;
        private TextBox tbMetadataVersion;
        private TextBox tbModel;
        private TextBox tbPassword;
        private TextBox tbScopes;
        private TextBox tbSearchScopes;
        private TextBox tbSerial;
        private TextBox tbType;
        private TextBox tbUsername;

        public DiscoveryPage()
        {
            this.InitializeComponent();
            this._controller = new DiscoveryController(this);
            this._controller.DeviceDiscovered += new Action<DeviceDiscoveryData>(this.OnDeviceDiscovered);
            this._controller.DiscoveryCompleted += new Action(this.OnDiscoveryCompleted);
            this._controller.DeviceInformationReceived += new ManagementServiceProvider.DeviceInformationReceived(this.OnDeviceInformationReceived);
            this._controller.DiscoveryError += new Action<Exception>(this.OnDiscoveryError);
            this.ClearServiceInfo();
            this.FillNetworkInterfaces();
            if (this.cmbNICs.Items.Count > 0)
            {
                this.cmbNICs.SelectedIndex = 0;
            }
        }

        private void AddDeviceToList(DeviceDiscoveryData device)
        {
            IPAddress address = null;
            IPAddress.TryParse(device.EndPointAddress, out address);
            if (address != null)
            {
                ListViewItem item = null;
                int count = this.lvDevices.Items.Count;
                for (int i = 0; i < this.lvDevices.Items.Count; i++)
                {
                    DeviceDiscoveryData tag = (DeviceDiscoveryData)this.lvDevices.Items[i].Tag;
                    int num3 = this.Compare(address.GetAddressBytes(), IPAddress.Parse(tag.EndPointAddress).GetAddressBytes());
                    if (num3 < 0)
                    {
                        count = i;
                        break;
                    }
                    if ((num3 == 0) && (tag.UUID == device.UUID))
                    {
                        item = this.lvDevices.Items[i];
                        break;
                    }
                }
                if (item == null)
                {
                    item = new ListViewItem(new string[] { device.EndPointAddress, device.UUID });
                    this.lvDevices.Items.Insert(count, item);
                }
                item.Tag = device;
                if (this._unicastProbing)
                {
                    this.ShowDeviceDiscoveryData(device);
                }
                if (!item.Selected && this._autoSelectDevice)
                {
                    this.lvDevices.SelectedItems.Clear();
                    item.Selected = true;
                    this.lvDevices.Refresh();
                }
            }
        }

        private void btnCheckIP_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.tbDeviceIP.Text))
            {
                try
                {
                    IPAddress iP = DiscoveryUtils.GetIP(this.tbDeviceIP.Text, false);
                    if (iP != null)
                    {
                        this.ClearServiceInfo();
                        this._autoSelectDevice = true;
                        this._probeAnswered = false;
                        this._unicastProbing = true;
                        NICListItem selectedItem = (NICListItem)this.cmbNICs.SelectedItem;
                        this.Controller.ProbeDevice(selectedItem.NIC.IP, iP);
                    }
                    else
                    {
                        this.ShowError("Invalid IP address or hostname");
                    }
                }
                catch (Exception exception)
                {
                    this.ShowError(exception);
                }
            }
        }

        private void btnCheckService_Click(object sender, EventArgs e)
        {
            string text = this.cmbServiceAddress.Text;
            if (!string.IsNullOrEmpty(text))
            {
                try
                {
                    this.Controller.GetDeviceInformation(text);
                }
                catch (EndpointNotFoundException)
                {
                    this.ShowError("Could not connect to " + text);
                }
                catch (Exception exception)
                {
                    this.ShowError(exception);
                }
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearDUTInfo();
            this.ClearServiceInfo();
        }

        private void btnDiscover_Click(object sender, EventArgs e)
        {
            if (this.cmbNICs.SelectedItem != null)
            {
                try
                {
                    this.lvDevices.Items.Clear();
                    NICListItem selectedItem = (NICListItem)this.cmbNICs.SelectedItem;
                    this.Controller.RunDiscovery(selectedItem.NIC.IP);
                }
                catch (Exception exception)
                {
                    this.ShowError(exception);
                }
            }
        }

        private void checkHWStyle_CheckedChanged(object sender, EventArgs e)
        {
            int selectedIndex = this.cmbNICs.SelectedIndex;
            this.cmbNICs.Items.Clear();
            this.FillNetworkInterfaces();
            if (selectedIndex < this.cmbNICs.Items.Count)
            {
                this.cmbNICs.SelectedIndex = selectedIndex;
            }
        }

        private void chkShowSearchOptions_CheckedChanged(object sender, EventArgs e)
        {
            this.panelScopes.Visible = this.chkShowSearchOptions.Checked;
        }

        private void ClearDUTInfo()
        {
            this.cmbServiceAddress.Text = string.Empty;
            this.cmbServiceAddress.Items.Clear();
            this.btnCheckService.Enabled = false;
            this.tbDeviceIP.Text = string.Empty;
            this.tbScopes.Text = string.Empty;
            this.tbEpAddress.Text = string.Empty;
            this.tbMetadataVersion.Text = string.Empty;
            this.tbType.Text = string.Empty;
            this.lvDevices.SelectedItems.Clear();
        }

        private void ClearServiceInfo()
        {
            this.tbModel.Text = "<Press Check to get information>";
            this.tbFirmwareVersion.Text = "<Press Check to get information>";
            this.tbSerial.Text = "<Press Check to get information>";
            this.tbBrand.Text = "<Press Check to get information>";
        }

        private void cmbServiceAddress_Enter(object sender, EventArgs e)
        {
            this._prevAddress = this.cmbServiceAddress.Text;
        }

        private void cmbServiceAddress_Leave(object sender, EventArgs e)
        {
            if (this.cmbServiceAddress.Enabled && (string.Compare(this._prevAddress, this.cmbServiceAddress.Text, true) != 0))
            {
                string text = this.cmbServiceAddress.Text;
                Uri result = null;
                if (!Uri.TryCreate(text, UriKind.Absolute, out result))
                {
                    Uri.TryCreate("http://" + text, UriKind.Absolute, out result);
                }
                if (result != null)
                {
                    IPAddress address = null;
                    this.tbDeviceIP.Text = IPAddress.TryParse(result.Host, out address) ? address.ToString() : result.Host;
                }
                this.lvDevices.SelectedItems.Clear();
            }
        }

        private void cmbServiceAddress_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnCheckService.Enabled = !string.IsNullOrEmpty(this.cmbServiceAddress.Text);
            this.btnCheckService.Refresh();
        }

        private void cmbServiceAddress_TextUpdate(object sender, EventArgs e)
        {
            this.btnCheckService.Enabled = !string.IsNullOrEmpty(this.cmbServiceAddress.Text);
            this.btnCheckService.Refresh();
        }

        public int Compare(byte[] x, byte[] y)
        {
            if (x.Length != y.Length)
            {
                if (x.Length <= y.Length)
                {
                    return -1;
                }
                return 1;
            }
            for (int i = 0; i < x.Length; i++)
            {
                if (x[i] != y[i])
                {
                    return x[i].CompareTo(y[i]);
                }
            }
            return 0;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void EnableControls(bool enable)
        {
            this.btnDiscover.Enabled = enable;
            this.btnDiscover.Refresh();
            this.btnCheckIP.Enabled = !string.IsNullOrEmpty(this.tbDeviceIP.Text) && enable;
            this.btnCheckIP.Refresh();
            this.btnCheckService.Enabled = !string.IsNullOrEmpty(this.cmbServiceAddress.Text) && enable;
            this.btnCheckService.Refresh();
            this.cmbNICs.Enabled = enable;
            this.cmbServiceAddress.Enabled = enable;
            this.tbUsername.Enabled = enable;
            this.tbPassword.Enabled = enable;
            this.tbDeviceIP.Enabled = enable;
            this.checkHWStyle.Enabled = enable;
            this.lvDevices.Enabled = enable;
            this.chkShowSearchOptions.Enabled = enable;
            this.tbSearchScopes.Enabled = enable;
            Cursor.Current = enable ? Cursors.Default : Cursors.WaitCursor;
        }

        private void FillNetworkInterfaces()
        {
            foreach (NetworkInterfaceDescription description in this._controller.GetNetworkInterfaces())
            {
                NICListItem item = new NICListItem(description, this.checkHWStyle.Checked);
                this.cmbNICs.Items.Add(item);
            }
        }

        public override IController GetController()
        {
            return this._controller;
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            this.cmbNICs = new ComboBox();
            this.lblNIC = new Label();
            this.gbDutInfo = new GroupBox();
            this.lblPassword = new Label();
            this.tbPassword = new TextBox();
            this.lblUsername = new Label();
            this.tbUsername = new TextBox();
            this.lblFirmwareVersion = new Label();
            this.tbFirmwareVersion = new TextBox();
            this.lblSerial = new Label();
            this.tbSerial = new TextBox();
            this.lblModel = new Label();
            this.tbModel = new TextBox();
            this.lblBrand = new Label();
            this.tbBrand = new TextBox();
            this.btnCheckService = new Button();
            this.lblMetadataVersion = new Label();
            this.tbMetadataVersion = new TextBox();
            this.cmbServiceAddress = new ComboBox();
            this.btnClear = new Button();
            this.btnCheckIP = new Button();
            this.lblScopes = new Label();
            this.tbScopes = new TextBox();
            this.lblDeviceServiceAddress = new Label();
            this.lblDeviceIP = new Label();
            this.tbDeviceIP = new TextBox();
            this.lblType = new Label();
            this.tbType = new TextBox();
            this.lblEpAddress = new Label();
            this.tbEpAddress = new TextBox();
            this.lvDevices = new ListView();
            this.hdrIP = new ColumnHeader();
            this.hdrUUID = new ColumnHeader();
            this.btnDiscover = new Button();
            this.checkHWStyle = new CheckBox();
            this.splitContainerDiscovery = new SplitContainer();
            this.panelSearch = new Panel();
            this.panelDiscovery = new Panel();
            this.panelScopes = new Panel();
            this.tbSearchScopes = new TextBox();
            this.lblSearchScopes = new Label();
            this.chkShowSearchOptions = new CheckBox();
            this.discoveryPageToolTip = new ToolTip(this.components);
            this.gbDutInfo.SuspendLayout();
            this.splitContainerDiscovery.Panel1.SuspendLayout();
            this.splitContainerDiscovery.Panel2.SuspendLayout();
            this.splitContainerDiscovery.SuspendLayout();
            this.panelSearch.SuspendLayout();
            this.panelDiscovery.SuspendLayout();
            this.panelScopes.SuspendLayout();
            base.SuspendLayout();
            this.cmbNICs.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.cmbNICs.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmbNICs.FormattingEnabled = true;
            this.cmbNICs.Location = new Point(0x2c, 0x11);
            this.cmbNICs.Name = "cmbNICs";
            this.cmbNICs.Size = new Size(0x12a, 0x15);
            this.cmbNICs.TabIndex = 1;
            this.lblNIC.AutoSize = true;
            this.lblNIC.Location = new Point(4, 20);
            this.lblNIC.Name = "lblNIC";
            this.lblNIC.Size = new Size(0x1c, 13);
            this.lblNIC.TabIndex = 9;
            this.lblNIC.Text = "NIC:";
            this.gbDutInfo.Controls.Add(this.lblPassword);
            this.gbDutInfo.Controls.Add(this.tbPassword);
            this.gbDutInfo.Controls.Add(this.lblUsername);
            this.gbDutInfo.Controls.Add(this.tbUsername);
            this.gbDutInfo.Controls.Add(this.lblFirmwareVersion);
            this.gbDutInfo.Controls.Add(this.tbFirmwareVersion);
            this.gbDutInfo.Controls.Add(this.lblSerial);
            this.gbDutInfo.Controls.Add(this.tbSerial);
            this.gbDutInfo.Controls.Add(this.lblModel);
            this.gbDutInfo.Controls.Add(this.tbModel);
            this.gbDutInfo.Controls.Add(this.lblBrand);
            this.gbDutInfo.Controls.Add(this.tbBrand);
            this.gbDutInfo.Controls.Add(this.btnCheckService);
            this.gbDutInfo.Controls.Add(this.lblMetadataVersion);
            this.gbDutInfo.Controls.Add(this.tbMetadataVersion);
            this.gbDutInfo.Controls.Add(this.cmbServiceAddress);
            this.gbDutInfo.Controls.Add(this.btnClear);
            this.gbDutInfo.Controls.Add(this.btnCheckIP);
            this.gbDutInfo.Controls.Add(this.lblScopes);
            this.gbDutInfo.Controls.Add(this.tbScopes);
            this.gbDutInfo.Controls.Add(this.lblDeviceServiceAddress);
            this.gbDutInfo.Controls.Add(this.lblDeviceIP);
            this.gbDutInfo.Controls.Add(this.tbDeviceIP);
            this.gbDutInfo.Controls.Add(this.lblType);
            this.gbDutInfo.Controls.Add(this.tbType);
            this.gbDutInfo.Controls.Add(this.lblEpAddress);
            this.gbDutInfo.Controls.Add(this.tbEpAddress);
            this.gbDutInfo.Dock = DockStyle.Fill;
            this.gbDutInfo.Location = new Point(0, 0);
            this.gbDutInfo.Name = "gbDutInfo";
            this.gbDutInfo.Size = new Size(0x1d3, 0x1db);
            this.gbDutInfo.TabIndex = 8;
            this.gbDutInfo.TabStop = false;
            this.gbDutInfo.Text = "Device Under Test Information";
            this.lblPassword.AutoSize = true;
            this.lblPassword.Location = new Point(6, 0x98);
            this.lblPassword.Name = "lblPassword";
            this.lblPassword.Size = new Size(0x38, 13);
            this.lblPassword.TabIndex = 0x27;
            this.lblPassword.Text = "Password:";
            this.tbPassword.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.tbPassword.Location = new Point(0x8f, 0x95);
            this.tbPassword.Name = "tbPassword";
            this.tbPassword.Size = new Size(0xfe, 20);
            this.tbPassword.TabIndex = 12;
            this.lblUsername.AutoSize = true;
            this.lblUsername.Location = new Point(6, 0x7e);
            this.lblUsername.Name = "lblUsername";
            this.lblUsername.Size = new Size(0x3f, 13);
            this.lblUsername.TabIndex = 0x26;
            this.lblUsername.Text = "User Name:";
            this.tbUsername.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.tbUsername.Location = new Point(0x8f, 0x7b);
            this.tbUsername.Name = "tbUsername";
            this.tbUsername.Size = new Size(0xfe, 20);
            this.tbUsername.TabIndex = 11;
            this.lblFirmwareVersion.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
            this.lblFirmwareVersion.AutoSize = true;
            this.lblFirmwareVersion.Location = new Point(6, 0x1a7);
            this.lblFirmwareVersion.Name = "lblFirmwareVersion";
            this.lblFirmwareVersion.Size = new Size(90, 13);
            this.lblFirmwareVersion.TabIndex = 0x23;
            this.lblFirmwareVersion.Text = "Firmware Version:";
            this.tbFirmwareVersion.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
            this.tbFirmwareVersion.Location = new Point(0x8f, 420);
            this.tbFirmwareVersion.Name = "tbFirmwareVersion";
            this.tbFirmwareVersion.ReadOnly = true;
            this.tbFirmwareVersion.Size = new Size(0x13e, 20);
            this.tbFirmwareVersion.TabIndex = 20;
            this.tbFirmwareVersion.Text = "<Press Check to get information>";
            this.lblSerial.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
            this.lblSerial.AutoSize = true;
            this.lblSerial.Location = new Point(6, 0x18d);
            this.lblSerial.Name = "lblSerial";
            this.lblSerial.Size = new Size(0x4c, 13);
            this.lblSerial.TabIndex = 0x21;
            this.lblSerial.Text = "Serial Number:";
            this.tbSerial.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
            this.tbSerial.Location = new Point(0x8f, 0x18a);
            this.tbSerial.Name = "tbSerial";
            this.tbSerial.ReadOnly = true;
            this.tbSerial.Size = new Size(0x13e, 20);
            this.tbSerial.TabIndex = 0x13;
            this.tbSerial.Text = "<Press Check to get information>";
            this.lblModel.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
            this.lblModel.AutoSize = true;
            this.lblModel.Location = new Point(6, 0x173);
            this.lblModel.Name = "lblModel";
            this.lblModel.Size = new Size(0x27, 13);
            this.lblModel.TabIndex = 0x1f;
            this.lblModel.Text = "Model:";
            this.tbModel.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
            this.tbModel.Location = new Point(0x8f, 0x170);
            this.tbModel.Name = "tbModel";
            this.tbModel.ReadOnly = true;
            this.tbModel.Size = new Size(0x13e, 20);
            this.tbModel.TabIndex = 0x12;
            this.tbModel.Text = "<Press Check to get information>";
            this.lblBrand.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
            this.lblBrand.AutoSize = true;
            this.lblBrand.Location = new Point(6, 0x159);
            this.lblBrand.Name = "lblBrand";
            this.lblBrand.Size = new Size(0x26, 13);
            this.lblBrand.TabIndex = 0x1d;
            this.lblBrand.Text = "Brand:";
            this.tbBrand.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
            this.tbBrand.Location = new Point(0x8f, 0x156);
            this.tbBrand.Name = "tbBrand";
            this.tbBrand.ReadOnly = true;
            this.tbBrand.Size = new Size(0x13e, 20);
            this.tbBrand.TabIndex = 0x11;
            this.tbBrand.Text = "<Press Check to get information>";
            this.btnCheckService.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnCheckService.Location = new Point(0x193, 0x5f);
            this.btnCheckService.Name = "btnCheckService";
            this.btnCheckService.Size = new Size(0x3a, 0x17);
            this.btnCheckService.TabIndex = 10;
            this.btnCheckService.Text = "Check";
            this.discoveryPageToolTip.SetToolTip(this.btnCheckService, "Get device information using address specified");
            this.btnCheckService.UseVisualStyleBackColor = true;
            this.btnCheckService.Click += new EventHandler(this.btnCheckService_Click);
            this.lblMetadataVersion.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
            this.lblMetadataVersion.AutoSize = true;
            this.lblMetadataVersion.Location = new Point(6, 0x13f);
            this.lblMetadataVersion.Name = "lblMetadataVersion";
            this.lblMetadataVersion.Size = new Size(0x5d, 13);
            this.lblMetadataVersion.TabIndex = 0x1a;
            this.lblMetadataVersion.Text = "Metadata Version:";
            this.tbMetadataVersion.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
            this.tbMetadataVersion.Location = new Point(0x8f, 0x13c);
            this.tbMetadataVersion.Name = "tbMetadataVersion";
            this.tbMetadataVersion.ReadOnly = true;
            this.tbMetadataVersion.Size = new Size(0x13e, 20);
            this.tbMetadataVersion.TabIndex = 0x10;
            this.cmbServiceAddress.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.cmbServiceAddress.FormattingEnabled = true;
            this.cmbServiceAddress.Location = new Point(0x8f, 0x60);
            this.cmbServiceAddress.Name = "cmbServiceAddress";
            this.cmbServiceAddress.Size = new Size(0xfe, 0x15);
            this.cmbServiceAddress.TabIndex = 9;
            this.cmbServiceAddress.SelectedIndexChanged += new EventHandler(this.cmbServiceAddress_SelectedIndexChanged);
            this.cmbServiceAddress.Leave += new EventHandler(this.cmbServiceAddress_Leave);
            this.cmbServiceAddress.Enter += new EventHandler(this.cmbServiceAddress_Enter);
            this.cmbServiceAddress.TextUpdate += new EventHandler(this.cmbServiceAddress_TextUpdate);
            this.btnClear.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.btnClear.Location = new Point(0x191, 0x1be);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new Size(60, 0x17);
            this.btnClear.TabIndex = 0x15;
            this.btnClear.Text = "Clear";
            this.discoveryPageToolTip.SetToolTip(this.btnClear, "Clear device information");
            this.btnClear.UseVisualStyleBackColor = true;
            this.btnClear.Click += new EventHandler(this.btnClear_Click);
            this.btnCheckIP.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnCheckIP.Location = new Point(0x193, 0x44);
            this.btnCheckIP.Name = "btnCheckIP";
            this.btnCheckIP.Size = new Size(0x3a, 0x17);
            this.btnCheckIP.TabIndex = 8;
            this.btnCheckIP.Text = "Probe";
            this.discoveryPageToolTip.SetToolTip(this.btnCheckIP, "Probe device");
            this.btnCheckIP.UseVisualStyleBackColor = true;
            this.btnCheckIP.Click += new EventHandler(this.btnCheckIP_Click);
            this.lblScopes.AutoSize = true;
            this.lblScopes.Location = new Point(6, 0xb1);
            this.lblScopes.Name = "lblScopes";
            this.lblScopes.Size = new Size(0x2e, 13);
            this.lblScopes.TabIndex = 15;
            this.lblScopes.Text = "Scopes:";
            this.tbScopes.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.tbScopes.Location = new Point(0x8f, 0xb1);
            this.tbScopes.Multiline = true;
            this.tbScopes.Name = "tbScopes";
            this.tbScopes.ReadOnly = true;
            this.tbScopes.ScrollBars = ScrollBars.Both;
            this.tbScopes.Size = new Size(0x13e, 0x84);
            this.tbScopes.TabIndex = 15;
            this.lblDeviceServiceAddress.AutoSize = true;
            this.lblDeviceServiceAddress.Location = new Point(6, 100);
            this.lblDeviceServiceAddress.Name = "lblDeviceServiceAddress";
            this.lblDeviceServiceAddress.Size = new Size(0x7c, 13);
            this.lblDeviceServiceAddress.TabIndex = 13;
            this.lblDeviceServiceAddress.Text = "Device Service Address:";
            this.lblDeviceIP.AutoSize = true;
            this.lblDeviceIP.Location = new Point(6, 0x4a);
            this.lblDeviceIP.Name = "lblDeviceIP";
            this.lblDeviceIP.Size = new Size(0x39, 13);
            this.lblDeviceIP.TabIndex = 11;
            this.lblDeviceIP.Text = "Device IP:";
            this.tbDeviceIP.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.tbDeviceIP.Location = new Point(0x8f, 0x47);
            this.tbDeviceIP.Name = "tbDeviceIP";
            this.tbDeviceIP.Size = new Size(0xfe, 20);
            this.tbDeviceIP.TabIndex = 7;
            this.tbDeviceIP.TextChanged += new EventHandler(this.tbDeviceIP_TextChanged);
            this.tbDeviceIP.Leave += new EventHandler(this.tbDeviceIP_Leave);
            this.tbDeviceIP.Enter += new EventHandler(this.tbDeviceIP_Enter);
            this.lblType.AutoSize = true;
            this.lblType.Location = new Point(6, 0x30);
            this.lblType.Name = "lblType";
            this.lblType.Size = new Size(0x22, 13);
            this.lblType.TabIndex = 9;
            this.lblType.Text = "Type:";
            this.tbType.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.tbType.Location = new Point(0x8f, 0x2d);
            this.tbType.Name = "tbType";
            this.tbType.ReadOnly = true;
            this.tbType.Size = new Size(0x13e, 20);
            this.tbType.TabIndex = 6;
            this.lblEpAddress.AutoSize = true;
            this.lblEpAddress.Location = new Point(6, 0x16);
            this.lblEpAddress.Name = "lblEpAddress";
            this.lblEpAddress.Size = new Size(0x41, 13);
            this.lblEpAddress.TabIndex = 7;
            this.lblEpAddress.Text = "EP Address:";
            this.tbEpAddress.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.tbEpAddress.Location = new Point(0x8f, 0x13);
            this.tbEpAddress.Name = "tbEpAddress";
            this.tbEpAddress.ReadOnly = true;
            this.tbEpAddress.Size = new Size(0x13e, 20);
            this.tbEpAddress.TabIndex = 5;
            this.lvDevices.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.lvDevices.Columns.AddRange(new ColumnHeader[] { this.hdrIP, this.hdrUUID });
            this.lvDevices.FullRowSelect = true;
            this.lvDevices.GridLines = true;
            this.lvDevices.Location = new Point(3, 0x23);
            this.lvDevices.MultiSelect = false;
            this.lvDevices.Name = "lvDevices";
            this.lvDevices.Size = new Size(0x14f, 0x102);
            this.lvDevices.TabIndex = 4;
            this.lvDevices.UseCompatibleStateImageBehavior = false;
            this.lvDevices.View = View.Details;
            this.lvDevices.SelectedIndexChanged += new EventHandler(this.lvDevices_SelectedIndexChanged);
            this.hdrIP.Text = "IP";
            this.hdrIP.Width = 0x7a;
            this.hdrUUID.Text = "UUID";
            this.hdrUUID.Width = 220;
            this.btnDiscover.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.btnDiscover.Location = new Point(3, 6);
            this.btnDiscover.Name = "btnDiscover";
            this.btnDiscover.Size = new Size(0x14f, 0x17);
            this.btnDiscover.TabIndex = 3;
            this.btnDiscover.Text = "Discover Devices";
            this.discoveryPageToolTip.SetToolTip(this.btnDiscover, "Discover devices in the network");
            this.btnDiscover.UseVisualStyleBackColor = true;
            this.btnDiscover.Click += new EventHandler(this.btnDiscover_Click);
            this.checkHWStyle.AutoSize = true;
            this.checkHWStyle.Location = new Point(0x2c, 0x2b);
            this.checkHWStyle.Name = "checkHWStyle";
            this.checkHWStyle.Size = new Size(0x89, 0x11);
            this.checkHWStyle.TabIndex = 2;
            this.checkHWStyle.Text = "Use Hardware Notation";
            this.checkHWStyle.UseVisualStyleBackColor = true;
            this.checkHWStyle.CheckedChanged += new EventHandler(this.checkHWStyle_CheckedChanged);
            this.splitContainerDiscovery.Dock = DockStyle.Fill;
            this.splitContainerDiscovery.Location = new Point(0, 0);
            this.splitContainerDiscovery.Name = "splitContainerDiscovery";
            this.splitContainerDiscovery.Size = new Size(0x335, 0x1db);
            this.splitContainerDiscovery.SplitterDistance = 350;
            this.splitContainerDiscovery.TabIndex = 10;
            this.splitContainerDiscovery.Panel1.Controls.Add(this.panelSearch);
            this.splitContainerDiscovery.Panel1.Controls.Add(this.checkHWStyle);
            this.splitContainerDiscovery.Panel1.Controls.Add(this.cmbNICs);
            this.splitContainerDiscovery.Panel1.Controls.Add(this.lblNIC);
            this.splitContainerDiscovery.Panel1MinSize = 300;
            this.splitContainerDiscovery.Panel2.Controls.Add(this.gbDutInfo);
            this.splitContainerDiscovery.Panel2MinSize = 400;
            this.panelSearch.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.panelSearch.Controls.Add(this.panelDiscovery);
            this.panelSearch.Controls.Add(this.panelScopes);
            this.panelSearch.Controls.Add(this.chkShowSearchOptions);
            this.panelSearch.Location = new Point(4, 0x47);
            this.panelSearch.Margin = new Padding(0);
            this.panelSearch.Name = "panelSearch";
            this.panelSearch.Size = new Size(0x158, 0x194);
            this.panelSearch.TabIndex = 12;
            this.panelDiscovery.Controls.Add(this.btnDiscover);
            this.panelDiscovery.Controls.Add(this.lvDevices);
            this.panelDiscovery.Dock = DockStyle.Fill;
            this.panelDiscovery.Location = new Point(0, 0x69);
            this.panelDiscovery.Name = "panelDiscovery";
            this.panelDiscovery.Size = new Size(0x158, 0x12b);
            this.panelDiscovery.TabIndex = 12;
            this.panelScopes.Controls.Add(this.tbSearchScopes);
            this.panelScopes.Controls.Add(this.lblSearchScopes);
            this.panelScopes.Dock = DockStyle.Top;
            this.panelScopes.Location = new Point(0, 0x11);
            this.panelScopes.Name = "panelScopes";
            this.panelScopes.Size = new Size(0x158, 0x58);
            this.panelScopes.TabIndex = 11;
            this.tbSearchScopes.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.tbSearchScopes.Location = new Point(3, 0x15);
            this.tbSearchScopes.Multiline = true;
            this.tbSearchScopes.Name = "tbSearchScopes";
            this.tbSearchScopes.ScrollBars = ScrollBars.Both;
            this.tbSearchScopes.Size = new Size(0x14f, 60);
            this.tbSearchScopes.TabIndex = 1;
            this.discoveryPageToolTip.SetToolTip(this.tbSearchScopes, "Scopes for Probe requests. Different scopes are separated by space or line break");
            this.lblSearchScopes.AutoSize = true;
            this.lblSearchScopes.Location = new Point(0, 8);
            this.lblSearchScopes.Name = "lblSearchScopes";
            this.lblSearchScopes.Size = new Size(0x2e, 13);
            this.lblSearchScopes.TabIndex = 0;
            this.lblSearchScopes.Text = "Scopes:";
            this.chkShowSearchOptions.AutoSize = true;
            this.chkShowSearchOptions.Checked = true;
            this.chkShowSearchOptions.CheckState = CheckState.Checked;
            this.chkShowSearchOptions.Dock = DockStyle.Top;
            this.chkShowSearchOptions.Location = new Point(0, 0);
            this.chkShowSearchOptions.Name = "chkShowSearchOptions";
            this.chkShowSearchOptions.Padding = new Padding(6, 0, 0, 0);
            this.chkShowSearchOptions.Size = new Size(0x158, 0x11);
            this.chkShowSearchOptions.TabIndex = 10;
            this.chkShowSearchOptions.Text = "Show search options";
            this.chkShowSearchOptions.UseVisualStyleBackColor = true;
            this.chkShowSearchOptions.CheckedChanged += new EventHandler(this.chkShowSearchOptions_CheckedChanged);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.splitContainerDiscovery);
            base.Name = "DiscoveryPage";
            base.Size = new Size(0x335, 0x1db);
            this.gbDutInfo.ResumeLayout(false);
            this.gbDutInfo.PerformLayout();
            this.splitContainerDiscovery.Panel1.ResumeLayout(false);
            this.splitContainerDiscovery.Panel1.PerformLayout();
            this.splitContainerDiscovery.Panel2.ResumeLayout(false);
            this.splitContainerDiscovery.ResumeLayout(false);
            this.panelSearch.ResumeLayout(false);
            this.panelSearch.PerformLayout();
            this.panelDiscovery.ResumeLayout(false);
            this.panelScopes.ResumeLayout(false);
            this.panelScopes.PerformLayout();
            base.ResumeLayout(false);
        }

        private void lvDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lvDevices.SelectedItems.Count > 0)
            {
                this.ClearServiceInfo();
                ListViewItem item = this.lvDevices.SelectedItems[0];
                DeviceDiscoveryData tag = (DeviceDiscoveryData)item.Tag;
                this.ShowDeviceDiscoveryData(tag);
            }
        }

        private void OnDeviceDiscovered(DeviceDiscoveryData device)
        {
            this._probeAnswered = true;
            base.Invoke(new Action<DeviceDiscoveryData>(this.AddDeviceToList), new object[] { device });
        }

        private void OnDeviceInformationReceived(string manufacturer, string model, string firmwareVersion, string serial, string hardwareId)
        {
            base.BeginInvoke(new Action(() => this.tbModel.Text = model));
            base.BeginInvoke(new Action(() => this.tbFirmwareVersion.Text = firmwareVersion));
            base.BeginInvoke(new Action(() => this.tbSerial.Text = serial));
            base.BeginInvoke(new Action(() => this.tbBrand.Text = manufacturer));
        }

        private void OnDiscoveryCompleted()
        {
            Action method = null;
            this._autoSelectDevice = false;
            if (!this._probeAnswered && this._unicastProbing)
            {
                if (method == null)
                {
                    method = () => MessageBox.Show(this, "Device did not respond", "No response", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                base.Invoke(method);
            }
            this._probeAnswered = false;
            this._unicastProbing = false;
        }

        public void OnDiscoveryError(Exception e)
        {
            if (this._unicastProbing)
            {
                this._probeAnswered = true;
                this.ShowError(e);
            }
        }

        private void ShowDeviceDiscoveryData(DeviceDiscoveryData device)
        {
            this.tbEpAddress.Text = device.UUID;
            this.tbDeviceIP.Text = device.EndPointAddress;
            this.cmbServiceAddress.Items.Clear();
            foreach (string str in device.ServiceAddresses)
            {
                this.cmbServiceAddress.Items.Add(str);
            }
            this.cmbServiceAddress.Text = (device.ServiceAddresses.Count > 0) ? device.ServiceAddresses[0] : string.Empty;
            this.tbType.Text = device.Type;
            string[] strArray = device.Scopes.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            this.tbScopes.Text = string.Empty;
            foreach (string str2 in strArray)
            {
                this.tbScopes.Text = this.tbScopes.Text + str2 + "\r\n";
            }
            this.tbMetadataVersion.Text = device.MetadataVersion.ToString();
        }

        public override void SwitchToState(TestTool.GUI.Enums.ApplicationState state)
        {
            Action method = null;
            Action action2 = null;
            if (state.IsActive())
            {
                if (method == null)
                {
                    method = () => this.EnableControls(false);
                }
                base.Invoke(method);
            }
            else
            {
                if (action2 == null)
                {
                    action2 = () => this.EnableControls(true);
                }
                base.Invoke(action2);
            }
        }

        private void tbDeviceIP_Enter(object sender, EventArgs e)
        {
            this._prevDeviceIp = this.tbDeviceIP.Text;
        }

        private void tbDeviceIP_Leave(object sender, EventArgs e)
        {
            if (this.tbDeviceIP.Enabled && (string.Compare(this._prevDeviceIp, this.tbDeviceIP.Text, true) != 0))
            {
                this.lvDevices.SelectedItems.Clear();
            }
        }

        private void tbDeviceIP_TextChanged(object sender, EventArgs e)
        {
            this.btnCheckIP.Enabled = !string.IsNullOrEmpty(this.tbDeviceIP.Text);
            this.btnCheckIP.Refresh();
        }

        public void UpdateButtons()
        {
            this.btnCheckIP.Enabled = !string.IsNullOrEmpty(this.tbDeviceIP.Text) && this.btnCheckIP.Enabled;
            this.btnCheckIP.Refresh();
            this.btnCheckService.Enabled = !string.IsNullOrEmpty(this.cmbServiceAddress.Text) && this.btnCheckService.Enabled;
            this.btnCheckService.Refresh();
        }

        public void UpdateFormTitle()
        {
            Form form = base.FindForm();
            if (form != null)
            {
                string[] strArray = form.Text.Split(new string[] { " -" }, StringSplitOptions.RemoveEmptyEntries);
                IPAddress deviceAddress = this.DeviceAddress;
                form.Text = (strArray.Length > 0) ? strArray[0] : string.Empty;
                if (deviceAddress != null)
                {
                    form.Text = form.Text + " - " + deviceAddress.ToString();
                }
            }
        }

        internal DiscoveryController Controller
        {
            get
            {
                return this._controller;
            }
        }

        public DeviceDiscoveryData Current
        {
            get
            {
                foreach (ListViewItem item in this.lvDevices.Items)
                {
                    DeviceDiscoveryData tag = item.Tag as DeviceDiscoveryData;
                    if (string.Compare(tag.EndPointAddress, this.tbDeviceIP.Text, true) == 0)
                    {
                        return tag;
                    }
                }
                return null;
            }
        }

        public IPAddress DeviceAddress
        {
            get
            {
                bool flag = ((this.NIC != null) && (this.NIC.IP != null)) && (this.NIC.IP.AddressFamily == AddressFamily.InterNetworkV6);
                return DiscoveryUtils.GetIP(this.tbDeviceIP.Text, flag);
            }
            set
            {
                this.tbDeviceIP.Text = (value != null) ? value.ToString() : string.Empty;
            }
        }

        public List<DeviceDiscoveryData> Devices
        {
            get
            {
                List<DeviceDiscoveryData> list = new List<DeviceDiscoveryData>();
                foreach (ListViewItem item in this.lvDevices.SelectedItems)
                {
                    list.Add(item.Tag as DeviceDiscoveryData);
                }
                return list;
            }
        }

        public NetworkInterfaceDescription NIC
        {
            get
            {
                NetworkInterfaceDescription nIC = null;
                if (this.cmbNICs.SelectedItem != null)
                {
                    return ((NICListItem)this.cmbNICs.SelectedItem).NIC;
                }
                if (this.cmbNICs.Items.Count > 0)
                {
                    nIC = ((NICListItem)this.cmbNICs.Items[0]).NIC;
                }
                return nIC;
            }
        }

        public string NICAddress
        {
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    foreach (object obj2 in this.cmbNICs.Items)
                    {
                        NICListItem item = obj2 as NICListItem;
                        if ((item.NIC.IP != null) && (item.NIC.IP.ToString() == value))
                        {
                            this.cmbNICs.SelectedItem = obj2;
                            break;
                        }
                    }
                }
            }
        }

        public string Password
        {
            get
            {
                return this.tbPassword.Text;
            }
            set
            {
                this.tbPassword.Text = value;
            }
        }

        public string SearchScopes
        {
            get
            {
                return this.tbSearchScopes.Text;
            }
            set
            {
                this.tbSearchScopes.Text = value;
            }
        }

        public string ServiceAddress
        {
            get
            {
                return this.cmbServiceAddress.Text;
            }
            set
            {
                this.cmbServiceAddress.Text = value;
            }
        }

        public bool ShowOptions
        {
            get
            {
                return this.chkShowSearchOptions.Checked;
            }
            set
            {
                this.chkShowSearchOptions.Checked = value;
            }
        }

        public string UserName
        {
            get
            {
                return this.tbUsername.Text;
            }
            set
            {
                this.tbUsername.Text = value;
            }
        }
    }
}

